package com.heima.search.service.impl;


import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.constants.RedisConstants;
import com.heima.common.dtos.Result;
import com.heima.common.exception.AppHttpCodeEnum;
import com.heima.common.exception.LeadNewsException;
import com.heima.common.trie.Trie;
import com.heima.model.search.dtos.UserSearchDto;
import com.heima.model.search.pojos.ApAssociateWords;
import com.heima.search.mapper.ApAssociateWordsMapper;
import com.heima.search.service.ApAssociateWordsService;
import com.heima.utils.common.JsonUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
@Service
@Transactional
public class ApAssociateWordsServiceImpl extends ServiceImpl<ApAssociateWordsMapper,ApAssociateWords> implements ApAssociateWordsService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public Result<List<ApAssociateWords>> loadWords(UserSearchDto dto) {
        try {
            //从redis查询联想词
            String associateWordStr = (String) redisTemplate.opsForValue().get(RedisConstants.ASSOCIATE_WORD_REDIS);

            List<ApAssociateWords> associateWords = null;

            if (StringUtils.isEmpty(associateWordStr)){
                //如果redis不存在，则查询数据库
                associateWords  = list();
                //保存到redis
                redisTemplate.opsForValue().set(RedisConstants.ASSOCIATE_WORD_REDIS, JsonUtils.toString(associateWords));
            }else {
                associateWords = JsonUtils.toList(associateWordStr,ApAssociateWords.class);
            }
            //使用所有联想词构建Trie树对象
            Trie trie = new Trie();
            if (CollectionUtils.isNotEmpty(associateWords)){
                associateWords.forEach(word ->{
                    trie.insert(word.getAssociateWords());
                });
            }

            List<String> list = trie.getData(dto.getSearchWords());

            ArrayList<ApAssociateWords> wordsList = new ArrayList<>();

            if (CollectionUtils.isNotEmpty(list)){
                list.forEach(str ->{
                    ApAssociateWords words = new ApAssociateWords();
                    words.setAssociateWords(str);
                    wordsList.add(words);
                });
            }
            return Result.ok(wordsList);
        } catch (Exception e) {
            e.printStackTrace();
            throw new LeadNewsException(AppHttpCodeEnum.SERVER_ERROR);
        }

//
//        if (dto.getPageSize() == 0 || dto.getPageSize() > 10){
//            dto.setPageSize(10);
//        }
//
//        IPage<ApAssociateWords> iPage = new Page<>(0,dto.getPageSize());
//        QueryWrapper<ApAssociateWords> queryWrapper = new QueryWrapper<>();
//        queryWrapper.like("associate_word",dto.getSearchWords());
//        queryWrapper.orderByDesc("created_time");
//
//        iPage = page(iPage, queryWrapper);
//        return Result.ok(iPage.getRecords());
    }
}
